
Partial Class RIAssessments_TaskTimes
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, _
                            ByVal e As System.EventArgs) _
                            Handles Me.Load

        Me.MessageLabel.Visible = False

        If Not Page.IsPostBack Then
            Me.LoadDropDownLists()
            Me.BindGrid()
        End If

    End Sub

    Protected Sub LoadDropDownLists()

        With Me.ActivitiesDropDownList
            .DataSource = Cdm.RIDataCore.Assessments.AssessmentActivity.ListBySort("Description")
            .DataTextField = "Description"
            .DataValueField = "AssessmentActivityId"
            .DataBind()
        End With

        With Me.FuncCharDropDownList
            .DataSource = Cdm.RIDataCore.LookupTables.LuFunctionalCharacteristic.ListBySort("Description")
            .DataTextField = "Description"
            .DataValueField = "FunctionalCharacteristicId"
            .DataBind()
        End With

    End Sub

    Protected Sub BindGrid()

        Dim assessmentActivityTime As Cdm.RIDataCore.Assessments.AssessmentActivityTime
        assessmentActivityTime = Cdm.RIDataCore.Assessments.AssessmentActivityTime.ListByAssessmentActivityId(Me.ActivitiesDropDownList.SelectedValue)
        If assessmentActivityTime IsNot Nothing Then
            Me.MinutesLabel.Text = assessmentActivityTime.UnitTime
            Me.FunctionalMinutesLabel.Text = assessmentActivityTime.FunctionalUnitTime
        End If

        Me.FuncCharGridView.DataSource = Cdm.RIDataCore.Assessments.AssessmentActivityCharacteristicDisplay.ListByAssessmentActivityId(Me.ActivitiesDropDownList.SelectedValue, "Description")
        Me.FuncCharGridView.DataBind()

    End Sub

    Protected Sub ActivitiesDropDownList_SelectedIndexChanged(ByVal sender As Object, _
                                                              ByVal e As System.EventArgs) _
                                                              Handles ActivitiesDropDownList.SelectedIndexChanged

        Me.BindGrid()

    End Sub

    Protected Sub AddButton_Click(ByVal sender As Object, _
                                  ByVal e As System.EventArgs) _
                                  Handles AddButton.Click

        Dim tran As New Gentle.Framework.Transaction
        Dim assessmentActivityCharacteristic As Cdm.RIDataCore.Assessments.AssessmentActivityCharacteristic
        assessmentActivityCharacteristic = New Cdm.RIDataCore.Assessments.AssessmentActivityCharacteristic(Me.ActivitiesDropDownList.SelectedValue, _
                                                                                                           Me.FuncCharDropDownList.SelectedValue)
        If Not Me.FuncCharExists(assessmentActivityCharacteristic.FunctionalCharacteristicId) Then
            assessmentActivityCharacteristic.Persist(tran)
            tran.Commit()
        Else
            tran.Rollback()
            Me.MessageLabel.Visible = True
            Me.MessageLabel.Text = "This functional characteristic already exists for this activity."
        End If

        Me.BindGrid()

    End Sub

    Protected Sub FuncCharGridView_RowCommand(ByVal sender As Object, _
                                              ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) _
                                              Handles FuncCharGridView.RowCommand

        If e.CommandName = "Remove" Then

            Dim assessmentActivityChar As Cdm.RIDataCore.Assessments.AssessmentActivityCharacteristic
            assessmentActivityChar = Cdm.RIDataCore.Assessments.AssessmentActivityCharacteristic.Retrieve(e.CommandArgument)
            assessmentActivityChar.Remove()
            Me.BindGrid()

        End If

    End Sub

    Protected Function FuncCharExists(ByVal currentFuncCharId As Integer) As Boolean

        For Each row As GridViewRow In Me.FuncCharGridView.Rows
            If currentFuncCharId = Me.FuncCharGridView.DataKeys(row.RowIndex).Values(1) Then
                Return True
            End If
        Next

        Return False

    End Function

End Class
